<?php
/**
 * Created by PhpStorm.
 * User: yuyj
 * Date: 2016-04-11
 * Time: 9:22
 */

namespace common\traits;

use common\support\LogHelper;
use yii;
use yii\helpers\Inflector;
use yii\log\Logger;

/**
 * Class FileLoggerTrait
 * @package common\traits
 * @property LogHelper $_fileLogger
 */
trait FileLoggerTrait
{
    private $_fileLogger = null;


    /**
     * 开启文件日志服务
     * @param null $logFile
     * @param int $flushInterval
     */
    public function enableFileLog($logFile = null, $flushInterval = 20)
    {
        if (empty($logFile)) {
            $logFile = $this->buildFileLogPath();
        }

        $fileLogger = Yii::createObject([
            'class'         => LogHelper::className(),
            'logFile'       => $logFile,
            'flushInterval' => $flushInterval,
        ]);

        $this->_fileLogger = $fileLogger->getLogger();
    }

    /**
     * 禁用文件日志服务
     */
    public function disableFileLog()
    {
        $this->_fileLogger = null;
    }

    /**
     * @return string
     */
    private function buildFileLogPath()
    {
        $class = get_called_class();
        $class = explode('\\', $class);
        $className = Inflector::camel2id(end($class), '_');

        return sprintf('%s/logs/%s/%s.log', Yii::$app->getRuntimePath(), $className, date('Y_m_d'));
    }

    /**
     * @return bool
     */
    public function checkFileLogger()
    {
        return $this->_fileLogger !== null;
    }

    /**
     * @param $message
     * @param array $data
     * @param null $category
     * @return bool|null
     */
    public function error($message, $data = [], $category = null)
    {
        return $this->filelog($message, $data, Logger::LEVEL_ERROR, $category);
    }

    /**
     * @param $message
     * @param array $data
     * @param null $category
     * @return bool|null
     */
    public function info($message, $data = [], $category = null)
    {
        return $this->filelog($message, $data, Logger::LEVEL_INFO, $category);
    }

    /**
     * @param $message
     * @param array $data
     * @param null $category
     * @return bool|null
     */
    public function warning($message, $data = [], $category = null)
    {
        return $this->filelog($message, $data, Logger::LEVEL_WARNING, $category);
    }

    /**
     * @param $message
     * @param array $data
     * @param null $category
     * @return bool|null
     */
    public function trace($message, $data = [], $category = null)
    {
        return $this->filelog($message, $data, Logger::LEVEL_TRACE, $category);
    }
    private function log($message, $params = [], $level = Logger::LEVEL_INFO, $category = null)
    {
        if (!$this->checkFileLogger()) {
            $this->enableFileLog();
        }
        if ($category === null) {
            $appPaths = explode("/", str_replace(['\\', '/'], '/', Yii::getAlias('@app')));
            $category = end($appPaths);
        }
        //格式化消息
        if ($params) {
            $message = vsprintf($message, $params);
        }
        $this->_fileLogger->log($message, $level, $category);
    }
    /**
     * @param $message
     * @param array $data
     * @param int $level
     * @param null $category
     * @return bool|null
     */
    private function filelog($message, $data = [], $level = Logger::LEVEL_INFO, $category = null)
    {
        if (!$this->checkFileLogger()) {
            return true;
        }

        if ($category === null) {
            $appPaths = explode("/", str_replace(['\\', '/'], '/', Yii::getAlias('@app')));
            $category = end($appPaths);
        }

        if (isset($data)) {
            $this->_fileLogger->log(
                [
                    'message' => $message,
                    'data'    => $data,
                ],
                $level,
                $category
            );
        } else {
            $this->_fileLogger->log($message, $level, $category);
        }

        return true;
    }
}
